package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.mystery;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.damgesource.SkillDamageSource;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.Ref;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.ArrayList;
import java.util.List;

public class Delaying extends BaseSkill {

    public List<DelayDamage> damages = new ArrayList<>();

    public Delaying(int level) {
        super(level);
    }

    public Delaying(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public Delaying() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "迟滞技能，受到的伤害不会立刻结算而是随后五秒内结算。";
    }

    @Override
    public SkillRarity getSkillRarity() {
        return SkillRarity.Mystery;
    }

    @Override
    public int getEffectiveSequence() {
        return 99;
    }

    @Override
    public boolean onDamaged(LivingEntity attacker, MobEntity target, DamageSource source, Ref<Float> amount, boolean ismob, boolean remote) {
        if (source instanceof SkillDamageSource source1 && source1.getPassSkill()) return true;
        DelayDamage damage = new DelayDamage(5, amount.getValue());
        amount.setValue(damage.getDamage());
        damages.add(damage);
        return true;
    }

    @Override
    public boolean passiveApply(MobEntity mobEntity) {
        if (mobEntity.world.getTime() % 20 == 0 && !damages.isEmpty()) {
            damages.forEach(damage -> mobEntity.damage(SkillDamageSource.PassSkillBaseDamage, damage.getDamage()));
            damages.removeIf(damages -> damages.damage == 0);
        }
        return true;
    }

    public class DelayDamage {
        public int delattime;
        public float damage;

        public DelayDamage(int t, float d) {
            delattime = t;
            damage = d;
        }

        public float getDamage() {
            if (delattime == 0) return 0;
            float ans = damage / delattime;
            damage -= ans;
            delattime--;
            return ans;
        }
    }
}
